
import { update } from './module/update.js';
import { CONF_CONFIG, PLAYERINFO_CONFIG } from './config.js';
import { ll_import } from './ll_import.js';
import * as utils from './utils/utils.js';
import { mainMenu } from './gui/main.js';
import { XYZ_PUBLIC, XYZ_PERSON } from './module/xyzData.js';
import { ReceivedXyz, DeadXyz } from './const.js';
import { ll_export } from './ll_export.js';
// 版本升级
update();
let Direction, Navigation;
mc.listen('onServerStarted',()=>{
    ({Direction, Navigation} = ll_import());
    registerCmd();
});
export {Direction, Navigation};

function registerCmd(){
    let cmd = mc.newCommand("xyz","广播与记录坐标", PermType.Any);
    //PermType.Any: 任何人,  PermType.GameMasters: 仅OP, PermType.Console: 仅控制台
    cmd.overload([]);
    cmd.setCallback((_cmd, _ori, out, res) => {
        let pl = _ori.player;
        if (pl == null){
            return out.error('此命令仅允许玩家执行');
        }
        mainMenu(pl);
        out.success();
    });
    cmd.setup();

    if (CONF_CONFIG.get("额外功能")["死亡坐标"]["启用回溯"] === true){
        let cmd = mc.newCommand("back","死亡回溯", PermType.Any);
        //PermType.Any: 任何人,  PermType.GameMasters: 仅OP, PermType.Console: 仅控制台
        cmd.overload([]);
        cmd.setCallback((_cmd, _ori, out, res) => {
            let pl = _ori.player;
            if (pl == null){
                return out.error('此命令仅允许玩家执行');
            }
            if (DeadXyz[pl.xuid] != undefined){
                pl.teleport(...Object.values(DeadXyz[pl.xuid].pos));
                pl.tell(`§a已回溯§r ${utils.buildXyzStr(DeadXyz[pl.xuid])}`);
                delete DeadXyz[pl.xuid];
            }else{
                pl.tell("§c无死亡坐标记录");
            }
            out.success();
        });
        cmd.setup();
    }
}

let tmp = {};
mc.listen("onUseItemOn",function(pl, item){
    if (pl.isSimulatedPlayer()){return;}
    if (tmp[pl.xuid]!=null){return;}
    tmp[pl.xuid] = true;
    setTimeout(()=>{
        delete tmp[pl.xuid];
    }, 300);

    // 设置方块的功能菜单
    if (item.type == CONF_CONFIG.get('表单')['物品']) {
        mainMenu(pl);
    }
});

mc.listen("onJoin", function(pl){
    if (pl.isSimulatedPlayer()){return;}
    // 更新玩家名称
    let playersInfo = PLAYERINFO_CONFIG.get("players");
    // if (playersInfo?.[pl.xuid]?.realName == pl.realName){
    //     return;
    // }
    playersInfo[pl.xuid] = {
        realName: pl.realName, 
        uuid: pl.uuid,
        xuid: pl.xuid
    };
    PLAYERINFO_CONFIG.set("players", playersInfo);

    XYZ_PERSON.init(pl.xuid);
});

mc.listen('onPlayerDie',function(pl,source){
    if (pl.isSimulatedPlayer()){return;}
    ReceivedXyz[pl.xuid] = {
        name: '§c§o死亡地点',
        tpAllow: false,
        time: new Date().getTime(),
        pos: {
            x: pl.blockPos.x,
            y: pl.blockPos.y,
            z: pl.blockPos.z,
            dimid: pl.blockPos.dimid
        }
    };
    DeadXyz[pl.xuid] = JSON.parse(JSON.stringify(ReceivedXyz[pl.xuid]));
    if (CONF_CONFIG.get("额外功能")["死亡坐标"]["启用通知"] === true){
        pl.tell(utils.buildXyzStr(ReceivedXyz[pl.xuid]));
    }
});

ll_export();
